package com.kuang.cas;

import org.junit.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author luomingliang
 * @date 2021/5/27 21:35
 */
public class CASDemo {
    //CAS
    public static void main(String[] args) throws InterruptedException {
        //正常的业务操作，这里面比较的都是一个个对象
        AtomicInteger atomicInteger = new AtomicInteger(2020);

        //期望，更新
        //public final boolean compareAndSet(int expect, int update)
        //如果我期望的值达到了，那么就更新，否则不更新
        //=======================捣乱的线程================================
        System.out.println(atomicInteger.compareAndSet(2020, 2021));
        System.out.println(atomicInteger.get());

        System.out.println(atomicInteger.compareAndSet(2021, 2020));
        System.out.println(atomicInteger.get());
        //=======================期望的线程================================
        System.out.println(atomicInteger.compareAndSet(2020, 6666));
        System.out.println(atomicInteger.get());
    }
}
